export const Address = {
  state: {
    address: [
      {
        id: 1,
        name: '张三',
        tel: 12345678911,
        address: '四川省成都市武侯区 肖家河街道',
        isDefault: true
      },
      {
        id: 2,
        name: '李四',
        tel: 12345678911,
        address: '四川省成都市武侯区 肖家河街道',
        isDefault: false
      }
    ]
  },
  mutations: {
    SETADDRESS (state, obj) {
      if (obj.isDefault === true) {
        state.address.forEach(item => { item.isDefault = false })
      }
      const index = state.address.findIndex(item => item.id === obj.id)
      state.address[index] = obj
    },
    DELADDRESS (state, id) {
      const index = state.address.findIndex(item => item.id === id)
      state.address.splice(index, 1)
    },
    ADDADDRESS (state, obj) {
      if (obj.isDefault === true) {
        state.address.forEach(item => { item.isDefault = false })
      }
      state.address.unshift(obj)
    }
  },
  actions: {
    setAddress ({ commit }, obj) {
      commit('SETADDRESS', obj)
    },
    delAddress ({ commit }, id) {
      commit('DELADDRESS', id)
    },
    addAddress ({ commit }, obj) {
      commit('ADDADDRESS', obj)
    }
  }
}
